package org.example.curriculum_design_6_16.controller;

import jakarta.validation.Valid;
import org.example.curriculum_design_6_16.entity.pojo.CompetitionCatalog;
import org.example.curriculum_design_6_16.entity.pojo.CompetitionParticipation;
import org.example.curriculum_design_6_16.entity.vo.CompetitionParticipationVo;
import org.example.curriculum_design_6_16.entity.vo.Result;
import org.example.curriculum_design_6_16.service.CompetitionCatalogService;
import org.example.curriculum_design_6_16.service.CompetitionParticipationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/competitionParticipation")
public class CompetitionParticipationController {

    @Autowired
    private CompetitionParticipationService participationService;

    @Autowired
    private CompetitionCatalogService competitionCatalogService;

    /**
     * 获取所有参赛记录
     * @return 参赛记录列表
     */
    @GetMapping
    public Result getAllParticipations() {
        try {
            return Result.success(participationService.getAllParticipations());
        } catch (Exception e) {
            return Result.error("获取所有参赛记录失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取参赛记录
     * @param id 参赛记录ID
     * @return 参赛记录详情
     */
    @GetMapping("/{id}")
    public Result<CompetitionParticipationVo> getParticipationById(@PathVariable Integer id) {
        try {
            return Result.success(participationService.getParticipationById(id));
        } catch (Exception e) {
            return Result.error("获取参赛记录失败: " + e.getMessage());
        }
    }

    /**
     * 根据学生ID获取参赛记录
     * @param studentId 学生ID
     * @return 该学生的参赛记录列表
     */
    @GetMapping("/student/{studentId}")
    public Result getParticipationsByStudentId(@PathVariable String studentId) {
        try {
            return Result.success(participationService.getParticipationsByStudentId(studentId));
        } catch (Exception e) {
            return Result.error("获取学生参赛记录失败: " + e.getMessage());
        }
    }

    /**
     * 根据学生ID和学年获取参赛记录
     * @param studentId 学生ID
     * @param academicYear 学年
     * @return 该学生指定学年的参赛记录列表
     */
    @GetMapping("/student/{studentId}/year/{academicYear}")
    public Result<List<CompetitionParticipationVo>> getParticipationsByStudentIdAndYear(@PathVariable String studentId, @PathVariable String academicYear) {
        try {
            return Result.success(participationService.getParticipationsByStudentIdAndYear(studentId, academicYear));
        } catch (Exception e) {
            return Result.error("获取学生学年参赛记录失败: " + e.getMessage());
        }
    }

    /**
     * 根据竞赛ID获取参赛记录
     * @param competitionId 竞赛ID
     * @return 该竞赛的所有参赛记录
     */
    @GetMapping("/competition/{competitionId}")
    public Result<List<CompetitionParticipationVo>> getParticipationsByCompetitionId(@PathVariable Integer competitionId) {
        try {
            return Result.success(participationService.getParticipationsByCompetitionId(competitionId));
        } catch (Exception e) {
            return Result.error("获取竞赛参赛记录失败: " + e.getMessage());
        }
    }

    /**
     * 添加参赛记录
     * @param participation 参赛记录信息
     * @return 操作结果
     */
    @PostMapping
    public Result<Integer> addParticipation(@RequestBody CompetitionParticipation participation) {
        try {
            return Result.success(participationService.addParticipation(participation));
        } catch (Exception e) {
            return Result.error("添加参赛记录失败: " + e.getMessage());
        }
    }

    /**
     * 更新参赛记录
     * @param participation 参赛记录信息
     * @return 操作结果
     */
    @PutMapping
    public Result<Integer> updateParticipation(@RequestBody CompetitionParticipation participation) {
        try {
            return Result.success(participationService.updateParticipation(participation));
        } catch (Exception e) {
            return Result.error("更新参赛记录失败: " + e.getMessage());
        }
    }

    /**
     * 删除参赛记录
     * @param id 参赛记录ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Integer> deleteParticipation(@PathVariable Integer id) {
        try {
            return Result.success(participationService.deleteParticipation(id));
        } catch (Exception e) {
            return Result.error("删除参赛记录失败: " + e.getMessage());
        }
    }

    /**
     * 审核通过参赛记录
     * @param id 参赛记录ID
     * @param reviewComment 审核意见
     * @return 操作结果
     */
    @PostMapping("/approve/{id}")
    public Result<Integer> approveParticipation(@PathVariable Integer id, @RequestParam String reviewComment) {
        try {
            return Result.success(participationService.approveParticipation(id, reviewComment));
        } catch (Exception e) {
            return Result.error("审核参赛记录失败: " + e.getMessage());
        }
    }

    /**
     * 审核拒绝参赛记录
     * @param id 参赛记录ID
     * @param reviewComment 审核意见
     * @return 操作结果
     */
    @PostMapping("/reject/{id}")
    public Result<Integer> rejectParticipation(@PathVariable Integer id, @RequestParam String reviewComment) {
        try {
            return Result.success(participationService.rejectParticipation(id, reviewComment));
        } catch (Exception e) {
            return Result.error("拒绝参赛记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/score/{studentId}")
    public Result getScore(@PathVariable String studentId, @RequestParam String academicYear) {
        return Result.success(participationService.getSum(studentId, academicYear));
    }
}